/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiDadosTipoVeiculo.java
 *
 * Created on 24/11/2011, 09:35:55
 */
package view.gui;

import controller.ControllerTipoVeiculo;
import javax.swing.JOptionPane;
import model.dominio.TipoVeiculo;

/**
 *
 * @author Braulio
 * Classe responsável pelo tratamento de dados de Tipo Veículo.
 * Nesta classe, apenas o Cadastro de um novo Tipo é efetuado, uma vez que a edição não faz sentido.
 */
public class GuiDadosTipoVeiculo extends JInternalFramePermissao {

    private TipoVeiculo tipoVeiculo;

    /** Creates new form GuiDadosTipoVeiculo */
    /**
     * Construtor da interface gráfica para Cadastro de Tipo Veículo.
     */
    public GuiDadosTipoVeiculo() {
        initComponents();
        tipoVeiculo = new TipoVeiculo();
    }

    public GuiDadosTipoVeiculo(TipoVeiculo tipo) {
        initComponents();
        tipoVeiculo = tipo;
        boxTipo.setText(tipoVeiculo.getTipo());
        boxPBase.setText(String.valueOf(tipoVeiculo.getPrecoBase()));
        boxPKm.setText(String.valueOf(tipoVeiculo.getPrecoKm()));
        boxTarifa.setText(String.valueOf(tipoVeiculo.getTaxaUnica()));
        jButton1.setText("Editar");
        this.setTitle("Editar Tipo de Veículo");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        boxTipo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        boxTarifa = new javax.swing.JTextField();
        boxPBase = new javax.swing.JTextField();
        boxPKm = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastrar tipo de veículo");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações do tipo"));
        jPanel1.setLayout(null);

        jLabel1.setText("Tipo*:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 30, 50, 30);

        boxTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxTipoActionPerformed(evt);
            }
        });
        jPanel1.add(boxTipo);
        boxTipo.setBounds(100, 30, 230, 30);

        jLabel2.setText("Preço Base*:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 70, 90, 30);

        jLabel3.setText("Preço por Km*:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 110, 100, 30);

        jLabel4.setText("Tarifa Única*:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(10, 150, 80, 30);

        boxTarifa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxTarifaActionPerformed(evt);
            }
        });
        jPanel1.add(boxTarifa);
        boxTarifa.setBounds(100, 150, 230, 30);

        boxPBase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxPBaseActionPerformed(evt);
            }
        });
        jPanel1.add(boxPBase);
        boxPBase.setBounds(100, 70, 230, 30);

        boxPKm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxPKmActionPerformed(evt);
            }
        });
        jPanel1.add(boxPKm);
        boxPKm.setBounds(100, 110, 230, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 350, 200);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/salvar.png"))); // NOI18N
        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(10, 210, 140, 60);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/imagens/cancelar.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(190, 210, 140, 60);

        setBounds(0, 0, 365, 315);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 
     * @param evt 
     * @throws Formato de moeda
     * Exceção lançada quando formato do preço base ou por quilometragem não corresponde ao correto.
     */
    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
    }//GEN-LAST:event_formInternalFrameActivated

    private void guardar() {
        if (boxTipo.getText().isEmpty() || boxPBase.getText().isEmpty() || boxPKm.getText().isEmpty() || boxTarifa.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Todos os campos são obrigatórios ! ", "ERRO", JOptionPane.ERROR_MESSAGE);
            return;
        }
        try {
            tipoVeiculo.setTipo(boxTipo.getText());
            tipoVeiculo.setPBase(Double.parseDouble(boxPBase.getText().replace(',', '.')));
            tipoVeiculo.setPKm(Double.parseDouble(boxPKm.getText().replace(',', '.')));
            tipoVeiculo.setTaxaUnica(Double.parseDouble(boxTarifa.getText().replace(',', '.')));
            if (ControllerTipoVeiculo.salvar(tipoVeiculo)) {
                JOptionPane.showMessageDialog(null, "Dados armazenados com sucesso.", "", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Erro na digitação do(s) valor(es)! ", "ERRO", JOptionPane.ERROR_MESSAGE);
            return;
        }
        this.dispose();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        guardar();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

    private void boxTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxTipoActionPerformed
        guardar();
    }//GEN-LAST:event_boxTipoActionPerformed

    private void boxPBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxPBaseActionPerformed
        guardar();
    }//GEN-LAST:event_boxPBaseActionPerformed

    private void boxTarifaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxTarifaActionPerformed
        guardar();
        }//GEN-LAST:event_boxTarifaActionPerformed

    private void boxPKmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxPKmActionPerformed
        guardar();
    }//GEN-LAST:event_boxPKmActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField boxPBase;
    private javax.swing.JTextField boxPKm;
    private javax.swing.JTextField boxTarifa;
    private javax.swing.JTextField boxTipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPermissao() {
        if (tipoVeiculo.getCodigo() > 0) {
            return 16;
        } else {
            return 15;
        }
    }
}
